package com.hdsx.restful.glzt.roadline.lxxx.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.esri.arcgis.geometry.IPoint;
import com.esri.arcgis.geometry.IPolyline;
import com.esri.arcgis.geometry.Polyline;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;
import com.hdsx.ao.base.BaseOpearte;
import com.hdsx.jts.service.RoadLineService;
import com.hdsx.jts.service.impl.arcgis.ArcRoadLineServiceImpl;
import com.hdsx.restful.glzt.roadline.lxxx.bean.Lx;
import com.hdsx.restful.glzt.roadline.lxxx.mapper.LxMapper;
import com.hdsx.restful.glzt.roadline.lxxx.service.LxPoadPosLonLatService;

// TODO: Auto-generated Javadoc
/**
 * The Class LxPoadPosLonLatServiceImpl.
 */
@Service
public class LxPoadPosLonLatServiceImpl extends BaseOpearte<Lx> implements LxPoadPosLonLatService {
	
	/**
	 * The Constructor.
	 */
	public LxPoadPosLonLatServiceImpl() {
		super("GIS_LX");//查询数据空间数据库表名
	}
	static
	{
		try 
		{
			EngineInitializer.initializeVisualBeans();
			AoInitialize ao= new AoInitialize();
			initializeArcGISLicenses(ao);
		} catch (Exception e) {
			//log.debug("\n"+"AO环境初始化失败请安装ArcGIS Desktop,启动ArcGIS Liense!");
			e.printStackTrace();
		}
	}
	
	/**
	 * Initialize arc gis licenses.
	 *
	 * @param aoInit the ao init
	 */
	private static void initializeArcGISLicenses(AoInitialize aoInit) {
		try {
			if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) 
					== esriLicenseStatus.esriLicenseAvailable)
				aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
			else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeBasic) 
					== esriLicenseStatus.esriLicenseAvailable)
				aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeBasic);
			else{
				System.err.println("Could not initialize an Engine or Basic License. Exiting application.");
				System.exit(-1);
			}
		} catch (Exception e) {e.printStackTrace();}
	}
	
	/** The mapper. */
	@Resource(name="lxMapper")
	private LxMapper mapper;

	/* (non-Javadoc)
	 * @see com.hdsx.restful.glzt.roadline.lxxx.service.LxPoadPosLonLatService#posByLonLat(java.lang.String, double, double, double, double)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public IPoint posByLonLat(String lxdm, double qdzh, double zdzh,double ptx,double pty) {
		Map<String,Object> parameter=new HashMap<String,Object>();
		parameter.put("lxdm", lxdm);
		parameter.put("qdzh", qdzh);
		parameter.put("zdzh", zdzh);
		try{
			Lx lx=mapper.posByLonLat(parameter);
			if(lx==null) return null;
			Polyline line=(Polyline) selectShape("ID='"+lx.getId()+"'");
			System.out.println("************"+line.getPoint(0).getM());
		    @SuppressWarnings("rawtypes")
			RoadLineService roadLineService=new ArcRoadLineServiceImpl();
			IPoint point=(IPoint) roadLineService.getPosByLonLat(line,ptx,pty);
			return point;
		}catch(Exception e){
			System.out.println("坐标转桩号过程中，查询路线信息出现异常"+e);
		}
		return null;
		
	}

	/* (non-Javadoc)
	 * @see com.hdsx.restful.glzt.roadline.lxxx.service.LxPoadPosLonLatService#lonLatByRoadPos(java.lang.String, double)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public IPoint lonLatByRoadPos(String lxdm, double pos) {
		Map<String,Object> parameter=new HashMap<String,Object>();
		parameter.put("lxdm", lxdm);
		parameter.put("pos", pos);
		try{
			Lx lx=mapper.lonLatByRoadPos(parameter);
			try{
				try{
					
					if(lx==null) return null;
					Polyline line=(Polyline) selectShape("ID='"+lx.getId()+"'");
					RoadLineService roadLineService=new ArcRoadLineServiceImpl();
					IPoint point=(IPoint) roadLineService.getLonLatByPos(line, pos);
					return point;
				}catch(Exception e){
					System.out.println("桩号转坐标过程出现异常"+e);
				}
			}catch(Exception e){
				System.out.println("桩号转坐标过程中，查询空间信息出现异常"+e);
			}
		}catch(Exception e){
			System.out.println("桩号转坐标过程中，查询路线信息出现异常"+e);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.hdsx.restful.glzt.roadline.lxxx.service.LxPoadPosLonLatService#getSegment(java.lang.String, double, double)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public String getSegment(String lxdm, double qdzh, double zdzh) {
		Map<String,Object> parameter=new HashMap<String,Object>();
		parameter.put("lxdm", lxdm);
		parameter.put("qdzh", qdzh);
		parameter.put("zdzh", zdzh);
		try{
			Lx lx=mapper.posByLonLat(parameter);
			if(lx==null) return null;
			Polyline line=(Polyline) selectShape("ID='"+lx.getId()+"'");
		    RoadLineService roadLineService=new ArcRoadLineServiceImpl();
			IPolyline polyline= (IPolyline) roadLineService.getSegment(line, qdzh, zdzh);
			String lineString=roadLineService.lineToString(polyline,false);
			return lineString;
		}catch(Exception e){
			System.out.println("坐标转桩号过程中，查询路线信息出现异常"+e);
		}
		return null;
	}
}
